package com.epro.dx.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.epro.dx.R;
import com.epro.dx.activity.BaseFragmentActivity;
import com.epro.dx.activity.SpecialActivity;
import com.epro.dx.util.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weipu.common.facade.model.BannerModel;

/**
 * 首页广告banner图片所在的fragment
 * 
 * @author  hjd008
 * @version  [版本号, 2014-12-13]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class DetailsPromotionFragment extends Fragment
{
    private static final String KEY_CONTENT = "MyPromotionFragment";
    
    private BannerModel bannerModel;
    
    
    private String smallUrlPic;
    
    private static Bitmap defaultBitmap;
    
    private Context mContext;
    
    private static int width;
    
    private DisplayImageOptions options;
    
    public static DetailsPromotionFragment newInstants(Context context, String smallUrl)
    {
        DetailsPromotionFragment newPreFragment = new DetailsPromotionFragment();
        newPreFragment.smallUrlPic = smallUrl;
        width=context.getResources().getDimensionPixelSize(R.dimen.product_detail_viewpager_height);
        return newPreFragment;
    }
    
    public DetailsPromotionFragment(){
    	options=ImageLoaderUtils.getImageLoaderOptions(R.drawable.item_default,R.drawable.item_default);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT))
        {
            bannerModel = (BannerModel)savedInstanceState.getSerializable(KEY_CONTENT);
        }
        
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        outState.putSerializable(KEY_CONTENT, bannerModel);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        ImageView imageView = new ImageView(getActivity());
        imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT/*width*/));
        //FIT_XY 把图片不按比例扩大/缩小到View的大小显示
        imageView.setScaleType(ScaleType.FIT_XY);
        String filePath = null;
       
        if (null != bannerModel)
        {
            filePath = bannerModel.getBannerForPhone1242X663();
        }
        else
        {
            filePath = smallUrlPic;
        }
        if (!TextUtils.isEmpty(filePath) && null != imageView)
        {
        	imageView.setTag(filePath);
//            imageWorker.loadImage(filePath, imageView);
        	ImageLoader.getInstance().displayImage(filePath, imageView, options);
        }
        else
        {
        	imageView.setImageBitmap(getDefault243());
        }
        
        imageView.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (null != bannerModel)
                {
                    // 如果触摸移动差值小于5，认为是单击
                    Intent intent = new Intent();
                    intent.putExtra("picmodel", bannerModel);
                    intent.setClass(getActivity(), SpecialActivity.class);
                    startActivity(intent);
                }
            }
        });
        
        return imageView;
    }
    
    /**
	 * 获取默认图片
	 * @param context
	 */
	 public Bitmap getDefault243()
	 {
		 if(null==defaultBitmap){
			 defaultBitmap = BitmapFactory.decodeResource((((BaseFragmentActivity)getActivity())).getResources(), R.drawable.default488_488);
		 }
		 return defaultBitmap;
	 }
    
    
    
}
